package com.leo.manage_system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.leo.manage_system.entity.Res;
import com.leo.manage_system.entity.vo.ResQueryVo;
import com.leo.manage_system.entity.vo.ResVo;
import com.leo.manage_system.mapper.ResMapper;
import com.leo.manage_system.service.ResService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 物品表 服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2021-11-01
 */
@Service
public class ResServiceImpl extends ServiceImpl<ResMapper, Res> implements ResService {

    final ResMapper resMapper;


    @Autowired
    public ResServiceImpl(ResMapper resMapper) {
        this.resMapper = resMapper;
    }

    @Override
    public boolean addRes(String strUserId, int intType, Res res) {
        try {
            resMapper.insert(res);

        } catch (Exception e) {
            return false;
        }
        return true;
    }

    @Override
    public ResVo getRes(String strResId) {
        Res res = resMapper.selectById(strResId);
        if (res == null) return null;

        ResVo resVo = new ResVo();
        resVo.copyFromRes(res);

        return resVo;
    }

    @Override
    public List<Res> getResList() {
        return null;
    }

    @Override
    public Map<String,Object> getResPageQuery(Page<Res> pageParam, ResQueryVo resQueryVo) {
        QueryWrapper<Res> queryWrapper = new QueryWrapper<>();
//        queryWrapper.select("user_id","res_type","name","picture","location","find_time");
        queryWrapper.orderByDesc("gmt_create");
        if( resQueryVo != null){
            if (StringUtils.isNotEmpty(resQueryVo.getUserId())) {
                queryWrapper.eq("user_id", resQueryVo.getUserId());
            }
            if (StringUtils.isNotEmpty(resQueryVo.getDescription())) {
                queryWrapper.like("description", resQueryVo.getDescription());
            }
            if (StringUtils.isNotEmpty(resQueryVo.getName())) {
                queryWrapper.like("name", resQueryVo.getName());
            }
        }

        resMapper.selectPage(pageParam,queryWrapper);
        System.out.println(pageParam.getRecords());
        List<Res> records = pageParam.getRecords();

        long current = pageParam.getCurrent();
        long pages = pageParam.getPages();
        long size = pageParam.getSize();
        long total = pageParam.getTotal();

        boolean hasNext = pageParam.hasNext();
        boolean hasPrevious = pageParam.hasPrevious();

        Map<String,Object> map = new HashMap<>();
        map.put("items",records);
        map.put("current",current);
        map.put("pages",pages);
        map.put("size",size);
        map.put("total",total);
        map.put("hasNext",hasNext);
        map.put("hasPrevious",hasPrevious);

        return map;

    }
//    @Override
//    public void getResPageQuery(Page<Res> pageParam, ResQuery query) {
//        QueryWrapper<Res> queryWrapper = new QueryWrapper<>();
//        queryWrapper.orderByAsc("sort");
//
//        if (query == null) {
//            resMapper.selectPage(pageParam, queryWrapper);
//            return;
//        }
//
//        String description = query.getDescription();
//        Date findTime = query.getFindTime();
//        String name = query.getName();
//
//        if (StringUtils.isNotEmpty(description)) {
//            queryWrapper.like("description", description);
//        }
//        if (StringUtils.isNotEmpty(name)) {
//            queryWrapper.like("name", description);
//        }
//
//        resMapper.selectPage(pageParam, queryWrapper);
//
//
//    }

}
